#include "stdafx.h"
#include "TextureManager.h"

#include "Log.h"
#include "Exception.h"

namespace Cvekas {

TextureManager::TextureManager(D3DDevicePtr device) 
	: 
device(device)
{
	LOG("TextureManager created");
}

TextureManager::~TextureManager()
{
	LOG("Destroying TextureManager");
}

Texture2DPtr TextureManager::loadTexture2D(const std::string &name, const std::string &file, D3DFORMAT format, 
										   bool pow_of2, bool lockable, bool gen_mipmaps)
{
	ResourceId id = getUniqueId(name);
	TexturePtr texture(new Texture2D(id, device, name, file, format, pow_of2, lockable, gen_mipmaps));
	return boost::static_pointer_cast<Texture2D>(insert(name, texture, id));
}

Texture2DPtr TextureManager::createTexture2D(const std::string &name, uint width, uint height, D3DFORMAT format, 
											 bool mipmaped, bool dynamic)
{
	ResourceId id = getUniqueId(name);
	TexturePtr texture(new Texture2D(id, device, name, width, height, format, mipmaped, dynamic));
	return boost::static_pointer_cast<Texture2D>(insert(name, texture, id));
}

TextureCubePtr TextureManager::loadTextureCube(const std::string& name, const std::string& file)
{
	ResourceId id = getUniqueId(name);
	TexturePtr texture(new TextureCube(id, device, name, file));
	return boost::static_pointer_cast<TextureCube>(insert(name, texture, id));
}

RenderTargetPtr TextureManager::createRenderTarget(const std::string& name, uint width, uint height, D3DFORMAT format)
{
	ResourceId id = getUniqueId(name);
	TexturePtr render_target(new RenderTarget(id, device, name, width, height, format));
	return boost::static_pointer_cast<RenderTarget>(insert(name, render_target, id));
}

Texture2DPtr TextureManager::getTexture2D(const std::string& name)
{
	TexturePtr texture = get(name);
	return boost::dynamic_pointer_cast<Texture2D>(texture);
}

Texture2DPtr TextureManager::getTexture2D(ResourceId id)
{
	TexturePtr texture = get(id);
	return boost::dynamic_pointer_cast<Texture2D>(texture);
}

TextureCubePtr TextureManager::getTextureCube(const std::string& name)
{
	TexturePtr texture = get(name);
	return boost::dynamic_pointer_cast<TextureCube>(texture);
}

TextureCubePtr TextureManager::getTextureCube(ResourceId id)
{
	TexturePtr texture = get(id);
	return boost::dynamic_pointer_cast<TextureCube>(texture);
}

RenderTargetPtr TextureManager::getRenderTarget(const std::string& name)
{
	TexturePtr texture = get(name);
	return boost::dynamic_pointer_cast<RenderTarget>(texture);
}

RenderTargetPtr TextureManager::getRenderTarget(ResourceId id)
{
	TexturePtr texture = get(id);
	return boost::dynamic_pointer_cast<RenderTarget>(texture);
}

void TextureManager::onDeviceLost()
{
	// If resources is empty, resources.begin() throws exception
	if(resources.empty())
		return;

	for(resources_itr = resources.begin(); resources_itr != resources.end(); ++resources_itr)
	{
		TexturePtr texture = *resources_itr;
		if(bool(texture))
			texture->onDeviceLost();
	}
}

void TextureManager::onDeviceReset()
{
	// If resources is empty, resources.begin() throws exception
	if(resources.empty())
		return;

	for(resources_itr = resources.begin(); resources_itr != resources.end(); ++resources_itr)
	{
		TexturePtr texture = *resources_itr;
		if(bool(texture))
			texture->onDeviceReset();
	}
}

} // namespace